package com.backend_pay.controller.user;

import com.backend_pay.entity.User;
import com.backend_pay.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Objects;

@Controller //控制层 展示层bean
public class registerController {
    //自动注入UserService
    @Autowired
    UserService userService;

    @PostMapping(value="/register")
    @ResponseBody
    /*public String register(@RequestParam("name") String name,
                           @RequestParam("password") String password){*/
    public String register(@RequestBody User userInfo){
            //添加user用户
            userService.savaNewUser(userInfo.getName(),userInfo.getPassword());
            return "true";
            /*user.setName(name);
            user.setPassword(password);
            Timestamp time =new Timestamp(System.currentTimeMillis());
            user.setCreatetime(time);
            user.setLastlogintime(time);
            //添加user用户
            userService.save(user);*/
    }

    //查询用户名是否存在
    @GetMapping(value="/userNameExist/{name}")
    @ResponseBody
    public String register(@PathVariable("name") String name){
        User user=new User();
        user=userService.findByName(name);
        if(!Objects.isNull(user)){
            return "用户名已存在";
        }
        else return "true";
    }
}
